import locales from './locales/index';
import registerLocales from "./utils/registerLocales";
registerLocales(locales)

import {properCase} from './utils/utils'
export * as decorator from '@common/decorator/wap'
const previewContext =  require['context']('../components', true, /\.((\/[a-zA-Z0-9]+){3})\/preview\/(wap\/index\.ts(x?))$/i, 'lazy')

const previewComponent = previewContext
.keys()
.reduce((modules: any, path: string) => {
    try {
    const key = path.split('/')[3];
    modules[properCase(key)] = () => previewContext(path);
    } catch (e) {
    console.error('PREVIREW COMPONENT_IMPORT_ERROR', path, e);
    }
    return modules;
}, {});
export default previewComponent

//@ts-ignore // 同步加载的组件
let previewContextSync: any = require['context']('../components', true, /\.\/form\/layout(\/SyncComponents)(\/[a-zA-Z0-9]+)\/preview\/(wap\/index\.ts(x?))$/i)

export const getSyncComponent = async () => {
    const syncComponent: Record<string, unknown> = {};
    await Promise.all(previewContextSync.keys().map(async (path: string) => {
        const key = path.split('/')[4];
        syncComponent[properCase(key)] = (await previewContextSync(path)).default;
    }))
    return syncComponent
}